package com.lujianfei.netease_android_viewpager2.widget

import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2

fun ViewPager2.setFlingDuration(duration: Int) {
    val recyclerView = getChildAt(0)
    if (recyclerView is RecyclerView) {
        val mPagerSnapHelperField = ViewPager2::class.java.getDeclaredField("mPagerSnapHelper")
        mPagerSnapHelperField.isAccessible = true
        val mPagerSnapHelper = CustomPagerSnapHelper(recyclerView = recyclerView, flingDuration = duration)
        recyclerView.onFlingListener = null
        mPagerSnapHelper.mIsFakeDragging = {
            isFakeDragging
        }
        mPagerSnapHelper.attachToRecyclerView(recyclerView)
        mPagerSnapHelperField.set(this, mPagerSnapHelper)
    }
}
